﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using Newtonsoft.Json;

namespace ProjectStudy.Ajax
{
    /// <summary>
    /// 随机获取3个不重复的城市 的摘要说明
    /// </summary>
    public class 随机获取3个不重复的城市 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            List<string> list = new List<string>();
            List<string> list2 = new List<string>();
            ArrayList arr = new ArrayList(3);
            string sql = "select * from City";
            DataTable dt = DBAccess.ExecuteDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                list.Add(dt.Rows[i]["CityName"].ConvertObjToStr());
            }

            Random r = new Random();

            while (arr.Count < 3)
            {
                int x = r.Next(0, list.Count);
                if (!arr.Contains(x))
                {
                    arr.Add(x);
                }
            }
            foreach (int item in arr)
            {
                list2.Add(list[item]);
            }
            context.Response.Write(JsonConvert.SerializeObject(list2));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}